package com.xxkj.drugtraceability.dialog

import android.content.Context
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.text.Editable
import android.text.TextWatcher
import android.util.DisplayMetrics
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.LinearLayout
import android.widget.PopupWindow
import androidx.databinding.DataBindingUtil
import androidx.recyclerview.widget.LinearLayoutManager
import com.xxkj.drugtraceability.R
import com.xxkj.drugtraceability.bean.DepartBean
import com.xxkj.drugtraceability.databinding.LayoutDepartPopBinding
import com.xxkj.drugtraceability.ext.clickN
import com.xxkj.drugtraceability.ext.windowManager
import com.xxkj.drugtraceability.ui.patientlist.DepartAdapter

/**
 * author: zhoujingjin
 * created on: 2024/9/13 18:41
 * description:科室选择的适配器
 */
class DepartPop(context: Context,private val list:List<DepartBean>,action:(item: DepartBean)->Unit):PopupWindow(context) {
    val adapter = DepartAdapter(action)
    init {
       val view = LayoutInflater.from(context).inflate(R.layout.layout_depart_pop,null)
       val bind = DataBindingUtil.bind<LayoutDepartPopBinding>(view)
        bind?.apply {
            recyclerView.layoutManager = LinearLayoutManager(context)
            recyclerView.adapter = adapter
            adapter.setList(list)
            tvCancel.clickN {
                dismiss()
            }
            etInput.addTextChangedListener(object:TextWatcher{
                override fun afterTextChanged(s: Editable?) {
                    if (!s.isNullOrEmpty()){
                        val filter =  list.filter {
                            it.deptName.contains(s.toString())
                        }
                        adapter.setList(filter)
                    }else{
                        adapter.setList(list)
                    }
                }

                override fun beforeTextChanged(
                    s: CharSequence?,
                    start: Int,
                    count: Int,
                    after: Int
                ) {
                }

                override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
                }
            })
        }
        contentView = view
        val displayMetrics = DisplayMetrics()
        context.windowManager?.defaultDisplay?.getMetrics(displayMetrics)
        val screenHeight = displayMetrics.heightPixels
        val maxHeight = (screenHeight * 0.7).toInt() // 4/5 of screen height
        // Ensure the popup's height doesn't exceed 4/5 of the screen
        height = if (view.measuredHeight > maxHeight) maxHeight else ViewGroup.LayoutParams.WRAP_CONTENT
        width =  ViewGroup.LayoutParams.MATCH_PARENT
        isFocusable = true
        setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))

    }

    override fun showAsDropDown(anchor: View?) {
        super.showAsDropDown(anchor)
    }
}